package view.CURD;

import library.controller.FrameListener;
import library.po.Book;
import library.po.User;
import library.service.BookService;
import library.service.BookTypeService;
import library.service.UserService;
import library.service.impl.BookServiceImpl;
import library.service.impl.BookTypeServiceImpl;
import library.service.impl.UserServiceImpl;
import view.main.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author unknown
 */
public class BookViewDialog extends JDialog {
    public BookViewDialog(MainFrame owner,String title,User user) {
        super(owner);
        this.user = userService.selectById(user.getUserId());
        this.setTitle(title);
        this.listener = owner.getMainListener();
        this.setVisible(true);
        initComponents();
    }

    private void initComponents() {
        adminViewBooksPanel = new JPanel();
        adminBookButtonBar = new JPanel();
        insertBookButton = new JButton();
        deleltBookButton = new JButton();
        updateBookButton = new JButton();
        selectBookButton = new JButton();
        outputBookButton = new JButton();
        scrollPane = new JScrollPane();
        bookTabel = new JTable();

        //======== this ========
        this.setLayout(new BorderLayout());

        //======== mouseClickMenu ========
        rightClickMenu = new JPopupMenu();
        copyItem = new JMenuItem();
        copyItem.setText("复制");
        copyItem.addActionListener(listener);
        copyItem.setActionCommand("CopyInfo");
        rightClickMenu.add(copyItem);

        //---- selectBookButton ----
        selectBookButton.setText("\u67e5\u8be2");
        selectBookButton.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 18));
        selectBookButton.addActionListener(listener);
        selectBookButton.setActionCommand("SelectBookDialog");
        selectBookButton.setBounds(0, 0, 105, 40);


        //======== scrollPane ========
        {
            bookTabel = this.getBookTable(bookService.selectAllBooks());
            scrollPane.setViewportView(bookTabel);
        }
        setSize(825, 505);
        setLocationRelativeTo(getOwner());

        //======== adminViewBooksPanel ========
        {
            adminViewBooksPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
            adminViewBooksPanel.setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax.
            swing. border. EmptyBorder( 0, 0, 0, 0) , "", javax. swing. border
            . TitledBorder. CENTER, javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069alog"
            ,java .awt .Font .BOLD ,12 ), java. awt. Color. red) ,adminViewBooksPanel. getBorder
            ( )) ); adminViewBooksPanel. addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java
            .beans .PropertyChangeEvent e) {if ("\u0062order" .equals (e .getPropertyName () )) throw new RuntimeException
            ( ); }} );
            adminViewBooksPanel.setLayout(null);

            //======== adminBookButtonBar ========
            {
                adminBookButtonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                adminBookButtonBar.setLayout(null);

                //---- insertBookButton ----
                insertBookButton.setText("\u6dfb\u52a0");
                insertBookButton.setFont(new Font("\u5e7c\u5706", insertBookButton.getFont().getStyle() & ~Font.BOLD, insertBookButton.getFont().getSize() + 6));
                insertBookButton.addActionListener(listener);
                insertBookButton.setActionCommand("InsertBookDialog");
                adminBookButtonBar.add(insertBookButton);
                insertBookButton.setBounds(520, 0, 105, 40);

                //---- deleltBookButton ----
                deleltBookButton.setText("\u5220\u9664");
                deleltBookButton.setFont(new Font("\u5e7c\u5706", deleltBookButton.getFont().getStyle() & ~Font.BOLD, deleltBookButton.getFont().getSize() + 6));
                deleltBookButton.addActionListener(listener);
                deleltBookButton.setActionCommand("DeleteBook");
                adminBookButtonBar.add(deleltBookButton);
                deleltBookButton.setBounds(160, 0, 105, 40);

                //---- updateBookButton ----
                updateBookButton.setText("\u4fee\u6539");
                updateBookButton.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 18));
                updateBookButton.addActionListener(listener);
                updateBookButton.setActionCommand("UpdateBookDialog");
                adminBookButtonBar.add(updateBookButton);
                updateBookButton.setBounds(350, 0, 105, 40);

                //---- selectBookButton ----
                adminBookButtonBar.add(selectBookButton);

                //---- outputBookButton ----
                outputBookButton.setText("\u5bfc\u51fa");
                outputBookButton.addActionListener(listener);
                outputBookButton.setActionCommand("Output");
                outputBookButton.setFont(new Font("\u5e7c\u5706", outputBookButton.getFont().getStyle() & ~Font.BOLD, outputBookButton.getFont().getSize() + 8));
                adminBookButtonBar.add(outputBookButton);
                outputBookButton.setBounds(695, 0, 100, 40);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < adminBookButtonBar.getComponentCount(); i++) {
                        Rectangle bounds = adminBookButtonBar.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = adminBookButtonBar.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    adminBookButtonBar.setMinimumSize(preferredSize);
                    adminBookButtonBar.setPreferredSize(preferredSize);
                }
            }
            adminViewBooksPanel.add(adminBookButtonBar);
            adminBookButtonBar.setBounds(12, 12, 799, adminBookButtonBar.getPreferredSize().height);

            //======== scrollPane ========
            {
                scrollPane.setViewportView(bookTabel);
            }
            adminViewBooksPanel.add(scrollPane);
            scrollPane.setBounds(12, 52, 799, 409);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < adminViewBooksPanel.getComponentCount(); i++) {
                    Rectangle bounds = adminViewBooksPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = adminViewBooksPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                adminViewBooksPanel.setMinimumSize(preferredSize);
                adminViewBooksPanel.setPreferredSize(preferredSize);
            }
        }
        if (this.getTitle().equals("总览图书")){
            adminViewBooksPanel.remove(adminBookButtonBar);
            adminBookButtonBar.remove(insertBookButton);
            adminBookButtonBar.remove(deleltBookButton);
            adminBookButtonBar.remove(updateBookButton);
            updateBookButton.setText("借阅");
            adminBookButtonBar.add(updateBookButton);
            adminViewBooksPanel.add(adminBookButtonBar);
            panel = adminViewBooksPanel;
            this.add(panel);
        }
        else if (this.getTitle().equals("图书管理")){
            panel = adminViewBooksPanel;
            this.add(panel);
        }
    }
    public JTable getBookTable(List<Book> bookList){
        String titles[] = {"书名", "ISBN", "状态", "作者" ,"库存" ,"类别"};
        Object data[][] = new Object[bookList.size()][6];
        Iterator<Book> iter = bookList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Book book = iter.next();
            data[i][0] = book.getBookName();
            data[i][1] = book.getBookIsbn();
            data[i][2] = this.typeToState(book.getBookState());
            data[i][3] = book.getBookWriter();
            data[i][4] = book.getBookReserve();
            data[i][5] = bookTypeService.selectByPrimaryKey(book.getBookType()).getBookTypeName();
            i++;
        }
        DefaultTableModel tableModel = new DefaultTableModel(data,titles);
        JTable table = new JTable(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(900, 600));
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
        table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
        table.setRowHeight(table.getRowHeight()+30); //表体高度
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
        table.repaint();

        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(final MouseEvent e) {
                int mods=e.getModifiers();
                table.add(rightClickMenu);
                //鼠标右键
                if((mods& InputEvent.BUTTON3_MASK)!=0){
                    //弹出菜单
                    rightClickMenu.show(table, e.getX(), e.getY());
                }
                selRow = table.getSelectedRow();  //得到选中的行
            }
        });
        return table;
    }
    public void bookRefresh(List<Book> bookList) {
        this.remove(panel);
        panel.remove(scrollPane);
        scrollPane.remove(bookTabel);
        bookTabel = this.getBookTable(bookList);
        scrollPane.setViewportView(bookTabel);
        panel.add(scrollPane);
        this.add(panel);
        this.validate();
    }
    public FrameListener getListener(){
        return this.listener;
    }
    private String typeToState(int type){
        if (type == 1)
            return "在馆";
        else if (type == 0)
            return "已借出";
        else
            return null;
    }
    public int getSelRow(){
        return this.selRow;
    }
    private JPopupMenu rightClickMenu;
    private JMenuItem copyItem;
    private JPanel adminViewBooksPanel;
    private JPanel adminBookButtonBar;
    private JButton insertBookButton;
    private JButton deleltBookButton;
    private JButton updateBookButton;
    private JButton selectBookButton;
    private JButton outputBookButton;
    private JScrollPane scrollPane;
    private JTable bookTabel;
    private FrameListener listener;
    private BookService bookService = new BookServiceImpl();
    private int selRow = -1;
    private JPanel panel;
    private BookTypeService bookTypeService = new BookTypeServiceImpl();
    private User user;
    private UserService userService = new UserServiceImpl();
}
